package com.nercel.dsj.gksales.api.controller;


import com.nercel.dsj.gksales.service.QiniuService;
import com.nercel.dsj.gksales.util.ApiResultUtil;
import com.nercel.dsj.gksales.util.UploadResp;
import com.nercel.dsj.gksales.util.UuidUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * @version 1.0
 * @author liangjing
 *
 * <p>Time: 2017年12月29日 下午2:19:44
 * <p>Description:  
 */
@RestController
@Api(tags="文件上传")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UploadApiController {
	
	private final QiniuService qiniuService;
	
	@Value("${upload.dir}")
	private String location;
	
	@Value("${qiniu.link}")
    private String qiniulink;

	@ApiOperation(value = "文件上传", notes = "本地保存后上传到七牛云")
    @RequestMapping(value="/api/v1/upload", method= RequestMethod.POST)
    public Object upload2(@RequestParam MultipartFile multipartFile, @RequestParam(defaultValue = "files") String type) throws Exception {
		String fileName = StringUtils.cleanPath(multipartFile.getOriginalFilename());
		long size = multipartFile.getSize();
		UploadResp res = qiniuService.upload(type+"/" + UuidUtil.get32UUID(), multipartFile);
//		User user = (User) SecurityUtils.getSubject().getPrincipal();
//		if (user != null) {
//			attachement.setUser(user);
//		}
    	return ApiResultUtil.success(qiniulink+res.key);
    }
}
